package com.pocket.insight.entity;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.pocket.insight.base.enums.AreaEnum;
import io.mybatis.provider.Entity;
import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 描述：
 *
 * @author shuize
 * @create 2024/12/24
 */
@Data
@Entity.Table(value = "app")
public class App implements Serializable {
    private static final long serialVersionUID = 1L;

    private Long          id;
    private String        title;
    private String        cover;
    private Options       options;
    private Long          createdBy;
    private LocalDateTime createdAt;
    private Long          updatedBy;
    private LocalDateTime updatedAt;
    private Long          latestAuthorizedBy;
    private LocalDateTime latestAuthorizedAt;
    private Long          latestPublishedBy;
    private LocalDateTime latestPublishedAt;
    private Boolean       visible;
    private Boolean       isDelete;
    private Boolean       isPublish;
    private Boolean       isBackup;
    private String        publishHash;
    private Long          publishedBy;
    private Long          hsVersion;
    private Long          folderId;
    private AreaEnum      area;
    private DataMode      dataMode;
    private Long          accessCount;
    private LocalDateTime lastAccessedAt;
    private Boolean       removeFolderId;
    private Long          designationCreatedBy;
    private AppTypeEnum   type;
    private List<Long>    dashboards;
    private List<Long>    datasets;
    private Long          tenantId;
    private Boolean       publishState;
    private List<Long>    hideDatasets;
    private Long          defaultDataset;
    private AppStatusEnum status;
    private LocalDateTime latestPendAt;

    @Data
    @JsonIgnoreProperties(ignoreUnknown = true)
    @JsonNaming(PropertyNamingStrategy.class)
    public static class Options implements Serializable {
        private static final long                 serialVersionUID         = 1L;

        private List<Long>           dashboards;
        private List<Long>           charts;
        private List<Long>           datasets;
        private List<Long>           dashboardsOrder;
        private String               appTemplateUUID;
        private Boolean              enableAppRule;
        private Boolean              enableRuleStrictValidate = false;
        private Boolean              enableFilp;
        private List<AppRelation>    dataAppDetails;
        private List<JSONObject>     filters;
        private Boolean              limitedCooperation;
        private Boolean              onlyDisplayForOwner;
        private JSONObject           pagination;
        private Boolean              enableDataAppAllocate;
        private List<AppRelation>    availableDataApps;
        private AreaEnum             source;
        private Map<String, Boolean> devices;
        private Integer              resultCacheInterval;
        private Boolean              useAppCustomCacheInterval;
    }

    public enum AppStatusEnum {
        PENDING,
        RUNNING;
        @JsonCreator
        public static AppStatusEnum fromString(String status) {
            try {
                return valueOf(status.toUpperCase());
            } catch (Exception e) {
                return null;
            }
        }
    }

    public enum AppTypeEnum {
        ANALYTIC_APP,
        QUERY_APP,
        DATA_APP;
    }

    public enum DataMode {
        APP_MODE,
        DATASET_MODE,
        VIEWER_MODE;
    }

}
